package com.zjw.zy.coreui.fragment

import android.graphics.Canvas
import android.graphics.Point
import android.os.Bundle
import android.view.View
import androidx.recyclerview.widget.GridLayoutManager
import com.zjw.zy.R
import com.zjw.zy.base.BaseFragment
import com.zjw.zy.base.BaseRecyclerAdapter
import com.zjw.zy.base.GestureRecyclerView
import com.zjw.zy.bean.ToolIcon
import com.zjw.zy.book.BookActivity
import com.zjw.zy.constant.ToolsName
import com.zjw.zy.coreui.fragment.adapter.ToolsAdapter
import com.zjw.zy.coreui.fragment.holder.ToolHolder
import com.zjw.zy.ext.finish
import com.zjw.zy.ext.go
import com.zjw.zy.ext.mainActivity
import com.zjw.zy.manager.PageManager
import com.zjw.zy.radio.RadioActivity
import com.zjw.zy.utils.BackHelper
import com.zjw.zy.utils.ResourcesUtils
import com.zjw.zy.utils.WindowUtils
import map.utils.MapUtils

class ToolsFragment : BaseFragment() {

    private val mList by lazy { findViewById<GestureRecyclerView>(R.id.tools_nav_list) }

    private val mAdapter = ToolsAdapter(object : BaseRecyclerAdapter.ItemListener<ToolHolder> {
        override fun onItemClick(holder: ToolHolder, v: View) {
            when (holder.data.name) {
                ToolsName.VOICE_TO_TEXT -> {
                    go(R.id.nav_recorder_fragment)
                }

                ToolsName.BROWSER -> {
                    go(R.id.nav_browser_fragment)
                }

                ToolsName.CRAMER -> {
                    go(R.id.nav_ocr_fragment)
                }

                ToolsName.MAP -> {
                    MapUtils.startMap()
                }

                ToolsName.BOOK -> {
                    PageManager.startActivity(BookActivity::class.java)
                }

                ToolsName.RADIO -> {
                    PageManager.startActivity(RadioActivity::class.java)
                }

                else -> {
                }
            }
        }

        override fun onItemLongClick(holder: ToolHolder, v: View): Boolean {
            v.startDragAndDrop(null, object : View.DragShadowBuilder() {
                override fun onProvideShadowMetrics(
                    outShadowSize: Point?,
                    outShadowTouchPoint: Point?
                ) {
                    outShadowSize?.let {
                        it.x = 1
                        it.y = 1
                    }

                    outShadowTouchPoint?.let {
                        it.x = 1
                        it.y = 1
                    }


                }

                override fun onDrawShadow(canvas: Canvas?) {
                    super.onDrawShadow(canvas)
                }
            }, null, 0)
            return false
        }

        override fun onItemDoubleClick(holder: ToolHolder, v: View) {

        }

    })

    override fun onPageEnter() {
        WindowUtils.TopBar.hideAllRightButton()
        WindowUtils.TopBar.showTopBarNav()
        WindowUtils.TopBar.setTopBarLabelText(ResourcesUtils.getString(R.string.tools_nav_label))
        WindowUtils.FootBar.show()

        mainActivity().setFullBackGround(R.color.memorandum_list_color)

    }

    override fun onViewRestore(savedInstanceState: Bundle?) {

    }

    override fun onSubscribe() {

    }

    override fun onBindViewListener() {

    }

    override fun onBindViewValue() {
        mList.adapter = mAdapter
        mList.layoutManager = GridLayoutManager(mainActivity(), 5)

        //todo test

        val list: MutableList<ToolIcon> = ArrayList()

        list.apply {
            this.add(ToolIcon(ToolsName.VOICE_TO_TEXT, 0, R.drawable.icon_recorder))
            this.add(ToolIcon(ToolsName.BROWSER, 1, R.drawable.icon_browser))
            this.add(ToolIcon(ToolsName.CRAMER, 2, R.drawable.icon_camera))
            this.add(ToolIcon(ToolsName.MAP, 3, R.drawable.icon_browser))
            this.add(ToolIcon(ToolsName.RADIO, 4, R.drawable.icon_browser))
            this.add(ToolIcon(ToolsName.BOOK, 5, R.drawable.icon_browser))
        }
        mAdapter.notifyData(list)
    }

    override fun onFindView(rootView: View?) {

    }

    override fun onLayoutId(): Int {
        return R.layout.fragment_tools_nav
    }

    override fun onPageExit() {

    }

    override fun onBackClick() {
        if (BackHelper.canExit()) {
            finish()
            return
        }
        BackHelper.markBackPressed()
    }
}